/*  This file is part of LitSearch.

    LitSearch is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    LitSearch is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with LitSearch; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */

package de.fhbrs.litsearch;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.*;

/**
 * Allows retrieving strings from the language flat files
 *
 * @version 	1.0 6 Jan 2006
 * @author 		Marco Werner
 * 
 */
public class LanguageBean {
	private final static String[] SUPPORTEDLANGUAGES = {"german", "english"};
	private String language, location, text;
	private ArrayList germanStrings;
	private ArrayList englishStrings;
	
	/** 
     * Set all instance variables to default values
     */
	public LanguageBean() {
		this.language = "german";
		this.location = "";
		this.text = "";
		this.germanStrings = new ArrayList();
		this.englishStrings = new ArrayList();
		this.readFlatFiles();
	}
	
	/** 
     * Get language as a string
     * @return Active language
     */
	public String getLanguage() {
		return this.language;
	}
	
	/** 
     * Set language
     * @param language Language as string (only supported languages)
     */
	public void setLanguage(String language) {
		if (Arrays.asList(SUPPORTEDLANGUAGES).contains(language.toLowerCase())) {
			this.language = language.toLowerCase();
		}
	}
	
	/** 
     * Get location of text in JSP-file as a string
     * @return Active location
     */
	public String getLocation() {
		return this.location;
	}
	
	/** 
     * Set location of text in JSP-file (i.e. home.welcome for the welcome text in home.jsp)
     * @param location Location in JSP-file
     */
	public void setLocation(String location) {
		this.location = location;
	}
	
	/**
	 * Get text at the specified location from Array
	 * @param location Location identifier for text as String
	 * @return String with text for the active location
	 */
	public String getTextFor(String location) {
		this.setLocation(location);
		this.queryArrayList();
		return this.text;
	}
	
	/** 
     * Get string for specified location from ArrayList
     */
	private void queryArrayList() {
		String line;
		Iterator iter;
		this.text = "";
		if (this.language.equals("german")) {
			iter = this.germanStrings.iterator();
		}
		else {
			iter = this.englishStrings.iterator();
		}
		while (iter.hasNext()) {
			line = iter.next().toString(); 
			if (line.startsWith(this.location)) {
				if (line.indexOf("=") == -1) {
					this.text = "<p class=\"debug\">[Error: Entry in File is malformed!]<br />DEBUG INFORMATION: Language=" + this.language + "   Location=" + this.location + "</p>";
				}
				else {
					this.text = line.substring(line.indexOf("=")+1);
				}
				break;
			}
		}
		if (this.text.length() == 0) {
			this.text = "<p class=\"debug\">[Error: This text can not be found!]<br />DEBUG INFORMATION: Language=" + this.language + "   Location=" + this.location + "</p>";
		}
	}
	
	/** 
     * Reads all strings from flat files
     */
	private void readFlatFiles() {
		String line;
		InputStream in1 = this.getClass().getResourceAsStream("german.lwt");
		try {
			BufferedReader br1 = new BufferedReader(new InputStreamReader(in1));
			while ((line = br1.readLine()) != null) {
				this.germanStrings.add(line);
			}
			in1.close();
		} catch(Exception e) { System.out.print("<p class=\"debug\">[Error: File with language specific strings could not be read!]<br />DEBUG INFORMATION: Language=" + this.language + "</p>"); }
		
		InputStream in2 = this.getClass().getResourceAsStream("english.lwt");
		try {
			BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
			while ((line = br2.readLine()) != null) {
				this.englishStrings.add(line);
			}
			in2.close();
		} catch(Exception e) { System.out.print("<p class=\"debug\">[Error: File with language specific strings could not be read!]<br />DEBUG INFORMATION: Language=" + this.language + "</p>"); }
	}
}
